package ua.kpi.ustymenko;
import ua.kpi.ustymenko.io.ClientIO;
import ua.kpi.ustymenko.factory.HardcodedQuestionsFactory;


public class ConsoleClientApp {
    private ClientIO clientIO = new ClientIO();
    private Tester tester = new Tester(new HardcodedQuestionsFactory());

    public void appStateIteration() {
        clientIO.printMainMenu();

        switch(clientIO.getMenuChoice()) {
            case 1:
                authorizationIteration();
                break;
            case 2:
                testIteration();
                break;
            case 3:
                System.exit(0);
            default:
                throw new IllegalArgumentException();
        }
    }

    private void authorizationIteration() {
        if (LoginHelper.getInst().isLogged()) {
            LoginHelper.getInst().logout();
        } else {
            LoginHelper.getInst().login();
        }
    }

    private void testIteration() {
        TestStats stats;
        if (LoginHelper.getInst().isLogged()) {
            stats = tester.takeTest(LoginHelper.getInst().getLoggedUser());
        } else {
            stats = tester.takeTest();
        }
        clientIO.writeStats(stats);
    }

    public static void main(String[] args) {
        ConsoleClientApp app = new ConsoleClientApp();
        while(true) {
            app.appStateIteration();
        }
    }
}

